﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DomMgmtSystem.View
{
    public partial class UserList : System.Web.UI.Page
    {
        string sqlWhere = " ";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (CommonCode.CurUser.NTYPE != 1)
                {
                    Response.Write("您当前没有权限访问该页面");
                    this.Controls.Clear();
                }
            }
        }
        /*分页*/
        public void GetPage()
        {
            /*求出总记录数*/
            int c = DataAccess.TB_USER.GetCount(sqlWhere);
            /*判断当前页面是否为0*/

            /*显示设置*/
            AspNetPager1.AlwaysShow = true;
            /*每页显示数量*/
            AspNetPager1.PageSize = 10;
            /*记录总页数*/
            AspNetPager1.RecordCount = c;
            /*绑定数据源*/
            ListDataBind();
            if (c > 0)
            {
                //dv1.Visible = false;
                Repeater1.Visible = true;
            }
            else
            {
                //dv1.Visible = true;
                Repeater1.Visible = false;
            }
        }

        /*分页*/
        protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            AspNetPager1.CurrentPageIndex = e.NewPageIndex;
            ListDataBind();
        }
        /*分页*/
        public void ListDataBind()
        {
            Repeater1.DataSource = DataAccess.TB_USER.GetList(sqlWhere, AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex - 1), AspNetPager1.PageSize);
            Repeater1.DataBind();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            sqlWhere = " 1=1 ";


            if (txtUserName.Text.Trim() != "")
                sqlWhere += " and CUSERNAME like '%{0}%'".FormatWith(txtUserName.Text.Trim());

            if (txtNickName.Text.Trim() != "")
                sqlWhere += string.Format(" and CNICKNAME like'%{0}%'", txtNickName.Text.Trim());

            ListDataBind();
        }

        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            decimal nID = Convert.ToDecimal(e.CommandArgument.ToString());
            if (e.CommandName == "Del")
            {
                DataAccess.TB_USER.Delete(nID);
                ListDataBind();
            }
            else if (e.CommandName == "RetPwd")
            {
                Model.TB_USER model = DataAccess.TB_USER.GetModel(nID);
                model.CPASSWORD = CommonCode.DEFAULTPWD;
                if (DataAccess.TB_USER.Update(model))
                    this.Alert("重置密码成功！");
                else
                    this.Alert("重置密码失败！");
            }
        }
    }
}